<?php


namespace App\Common;


use App\Common\Models\Db1\MpApp;
use App\Common\Models\Db1\MpAppMch;
use App\Common\Models\Db1\MpClick;
use App\Common\Models\Db1\MpLocation;
use App\Common\Models\Db1\MpPayment;
use App\Common\Models\Db1\MpText;

class WxUtil
{
    public static function event($message)
    {
        switch ($message['Event']) {
            case 'LOCATION':
                $item_location = new MpLocation();
                $item_location->from = $message['FromUserName'];
                $item_location->to = $message['ToUserName'];
                $item_location->create_time = date('Y-m-d H:i:s', $message['CreateTime']);
                $item_location->longitude = $message['Longitude'];
                $item_location->latitude = $message['Latitude'];
                $item_location->precision = $message['Precision'];
                $item_location->type = 'callback';
                $item_location->save();
                break;
            case 'CLICK':
                $item_location = new MpClick();
                $item_location->from = $message['FromUserName'];
                $item_location->to = $message['ToUserName'];
                $item_location->create_time = date('Y-m-d H:i:s', $message['CreateTime']);
                $item_location->event_key = $message['EventKey'];
                $item_location->type = 'callback';
                $item_location->save();
                break;
            default:
                break;
        }
    }

    public static function text($message)
    {
        $item_location = new MpText();
        $item_location->from = $message['FromUserName'];
        $item_location->to = $message['ToUserName'];
        $item_location->create_time = date('Y-m-d H:i:s', $message['CreateTime']);
        $item_location->content = $message['Content'];
        $item_location->type = 'callback';
        $item_location->save();
    }

    public static function getWxConfig($app_id) {
        $ret = [];
        $item_app = (new MpApp())->findOne(['app_id' => $app_id]);
        //LogUtil::info('test', [$app_id, $item_app], __FILE__, __LINE__);
        if (!empty($item_app->id)) {
            $ret = [
                'app_id' => $item_app->app_id,
                'app_secret' => $item_app->app_secret,
            ];
            //LogUtil::info('test', [$ret], __FILE__, __LINE__);
            $item_app_mch = (new MpAppMch())->findOne(['app_id' => $item_app->app_id]);
            if (!empty($item_app_mch->id)) {
                $item_payment = (new MpPayment())->findOne(['mchid' => $item_app_mch->mchid]);
                if (!empty($item_payment->id)) {
                    $ret['mchid'] = $item_payment->mchid;
                    $ret['partner_key'] = $item_payment->partner_key;

                    $certs_dir = sprintf(storage_path() . '/certs/%s/', $item_payment->mchid);
                    if (!file_exists($certs_dir)) {
                        mkdir($certs_dir, 0777, true);
                    }
                    $pem_file = sprintf(storage_path() . '/certs/%s/apiclient_cert.pem', $item_payment->mchid);
                    if (!file_exists($pem_file)) {
                        file_put_contents($pem_file, $item_payment->apiclient_cert);
                        chmod($pem_file, 0777);
                    }
                    $ret['apiclient_cert'] = $pem_file;

                    $pem_file = sprintf(storage_path() . '/certs/%s/apiclient_key.pem', $item_payment->mchid);
                    if (!file_exists($pem_file)) {
                        file_put_contents($pem_file, $item_payment->apiclient_key);
                        chmod($pem_file, 0777);
                    }
                    $ret['apiclient_key'] = $pem_file;
                }
            }
        }
        return $ret;
    }
}
